﻿using DialogShowSingle.IServices;
using Prism.Services.Dialogs;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DialogShowSingle.Services
{
    public class DialogManager : IDialogManager
    {
        private readonly IDialogService _dialogService;
        private readonly ConcurrentDictionary<string, bool> _dialogStatus = new();

        public DialogManager(IDialogService dialogService)
        {
            _dialogService = dialogService;
        }

        public void ShowDialog(string dialogName, DialogParameters parameters = null, Action<IDialogResult> callback = null)
        {
            if (_dialogStatus.TryGetValue(dialogName, out var isOpen) && isOpen)
                return;

            _dialogStatus[dialogName] = true; // 标记为已打开

            // 调用Prism原生弹窗服务
            _dialogService.ShowDialog(dialogName, parameters, result =>
            {
                _dialogStatus[dialogName] = false; // 关闭后重置状态
                callback?.Invoke(result);
            });
        }
    }
}
